﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VTV.TaskManagementSystem.Entities;

namespace VTV.TaskManagementSystem.Common
{
    public class RenderHTML
    {
        public static string Tasks(IEnumerable<ShowMe> showMes)
        {
            try
            {
                var result = "";
                showMes.ToList().ForEach(me => result += Task(me));

                return result;
            }
            catch (Exception e)
            {
                return null;
            }
        }

        private static string Task(ShowMe showMe)
        {
            string result;

            result = "<div class='task'>" +
                        "<div class='tasks-header'>" +
                            "<span class='header-text'>" + showMe.TaskName + "</span>" +
                        "</div>" +
                        "<div class='task-left'>" +
                            "<div class='task-commend'>" +
                                "<p class='task-commend-head'>Mô tả chung:</p>" + showMe.TaskDescription +
                            "</div>" +
                        "</div>" +
                        "<div class='task-right'>{is1}</div>" +
                    "</div>";

            string teams = "";
            foreach (var teamToShow in showMe.TeamToShows)
            {
                teams += TeamToShow(teamToShow);
            }

            result = result.Replace("{is1}", teams);

            return result;
        }

        private static string TeamToShow(TeamToShow entity)
        {
            string result;

            result = "<div class='task-team {status-shadow}'>" +
                        "<div class='task-team-header {status-header}'>" +
                            "<span class='team-header-text {status-color}'>" + entity.TeamName + "</span>" +
                        "</div>" +
                        "<div class='task-team-info'>" +
                            "<div class='task-team-time'>" +
                                "<p style='margin: 10px; font-size: 16px; font-weight: bold; color: #333333;'>Thông tin:</p>" +
                                "<div class='team-item'>" +
                                "<div class='lable-field'>Ngày bắt đầu:</div>" + (entity.StartDate.HasValue ? entity.StartDate.Value.ToString("dd/MM/yyyy") : string.Empty) +
                                "</div>" +
                                "<div class='team-item'>" +
                                    "<div class='lable-field'>Ngày kết thúc:</div>" + (entity.ExpectedEndDate.HasValue ? entity.ExpectedEndDate.Value.ToString("dd/MM/yyyy") : string.Empty) +
                                "</div>" +
                                "<div class='team-item'>" +
                                    "<div class='lable-field'>Trạng thái:</div>" +
                                    "<span class='task-team-info-status {status-textShadow}'>{status-value}</span>" +
                                "</div>" +
                            "</div>" +
                            "<div class='task-team-member'>" +
                                "<p style='margin: 10px; font-size: 16px; font-weight: bold; color: #333333;'>Thành viên:</p>" +
                                "<div class='team-member-name'>" +
                                    "<ol>{member-value}</ol>" +
                                "</div>" +
                            "</div>" +
                        "</div>" +
                        "<div class='task-team-comment'>" +
                            "<p style='margin: 0 10px 10px 5px; font-size: 16px; font-weight: bold; color: #333333;'>Ghi chú:</p>" + entity.Comment +
                        "</div>" +
                    "</div>";

            string employees = "";
            foreach (var employeeToShow in entity.EmployeeToShows)
            {
                var isTeamLead = employeeToShow.IsTeamLead.HasValue && employeeToShow.IsTeamLead.Value ? "class='team-member-name-leader'" : string.Empty;
                employees += "<li " + isTeamLead + ">" + employeeToShow.EmployeeName + "</li>";
            }

            result = result.Replace("{member-value}", employees);

            switch (entity.Status)
            {
                case 'D':
                case 'L':
                    result = result.Replace("{status-shadow}", "status-done-shadow");
                    result = result.Replace("{status-header}", "status-done-header");
                    result = result.Replace("{status-color}", "status-done-color");
                    result = result.Replace("{status-textShadow}", "status-done-textShadow");
                    result = result.Replace("{status-value}", "Hoàn thành");
                    break;
                case 'I':
                    result = result.Replace("{status-value}", "Đang hoàn thành");
                    if (DateTime.Today == entity.ExpectedEndDate)
                    {
                        result = result.Replace("{status-shadow}", "status-deadline-shadow");
                        result = result.Replace("{status-header}", "status-deadline-header");
                        result = result.Replace("{status-color}", "status-deadline-color");
                        result = result.Replace("{status-textShadow}", "status-deadline-textShadow");
                    }
                    else if (DateTime.Today > entity.ExpectedEndDate)
                    {
                        result = result.Replace("{status-shadow}", "status-late-shadow");
                        result = result.Replace("{status-header}", "status-late-header");
                        result = result.Replace("{status-color}", "status-late-color");
                        result = result.Replace("{status-textShadow}", "status-late-textShadow");
                    }
                    break;
            }

            return result;
        }
    }
}
